package com.dongdongshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.domain.AjaxResult;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.*;
import com.dongdongshop.vo.SpecVO;
import em.ResuleEnum;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("goodsedit")
public class GoodsController {

    @DubboReference
    private ItemcatService itemcatService;
    @DubboReference
    private TypeTemplateService typeTemplateService;
    @DubboReference
    private GoodsService goodsService;
    @DubboReference
    private SperOptionService specOptionService;


    @RequestMapping("goodseditShow")
    public String goodseditList(){
        return "admin/goods_edit";
    }

    @RequestMapping("itemcatList")
    @ResponseBody
    public AjaxResult itemcatList(@RequestParam(defaultValue = "0")Long parentId){
        List<TbItemCat> list = itemcatService.itemcatList1(parentId);
        return AjaxResult.R(ResuleEnum.SUCCESS).setData(list);
    }

    @RequestMapping("selectTypeTemplate")
    @ResponseBody
    public AjaxResult selectTypeTemplate(Long typeId){
        TbTypeTemplate tbTypeTemplate = typeTemplateService.selectById(typeId);
        return AjaxResult.R(ResuleEnum.SUCCESS).setData(tbTypeTemplate);
    }

    @RequestMapping("selectSpec")
    @ResponseBody
    public AjaxResult selectSpec(Long id){
        TbTypeTemplate tbTypeTemplate = typeTemplateService.selectById(id);
        String specIds = tbTypeTemplate.getSpecIds();
        List<SpecVO> specVOList  = JSONObject.parseArray(specIds,SpecVO.class);
        for (SpecVO specVO : specVOList) {
            List<TbSpecificationOption> optionList = specOptionService.specOptionBySpecIds(specVO.getId());
            specVO.setOptionList(optionList);
        }
        return AjaxResult.R(ResuleEnum.SUCCESS).setData(specVOList);
    }

    @RequestMapping("addGoods")
    @ResponseBody
    public AjaxResult addGoods(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc , TbItem item){
        TbSeller tbSeller =(TbSeller) SecurityUtils.getSubject().getPrincipal();
        tbGoods.setSellerId(tbSeller.getSellerId());
        item.setSellerId(tbSeller.getSellerId());
        goodsService.addGoods(tbGoods,tbGoodsDesc,item);
        return AjaxResult.R(ResuleEnum.SUCCESS);
    }

    @RequestMapping("kuozhanList")
    @ResponseBody
    public AjaxResult kuozhanList(Long customId){
        TbItemCat tbItemCat = itemcatService.selectById(customId);
        TbTypeTemplate tbTypeTemplate = typeTemplateService.selectById(tbItemCat.getTypeId());
        return AjaxResult.R(ResuleEnum.SUCCESS).setData(tbTypeTemplate);

    }
}
